<?php

class Usuarios extends CI_Controller {

    function Usuarios() {
        parent::__construct();
		$this->load->model('ws/usuario_model');
		$this->load->model('ws/perfil_model');
        session_start();
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('error', 'Debe haberse logueado');
            redirect('login', 'refresh');
        }
    }
	
	public function index() {
		$data['map'] = null;
		$data['title'] = "Itrade - Administraci&oacute;n";
        $data['username'] = $this->session->userdata('username');
        $data['name'] = $this->session->userdata('name');
        $data['acceso'] = $this->session->userdata('acceso');
        $data['main'] = "pages/dashboard_content.php"; //Ruta del contenido del home
		
		$data['usuarios'] = $this->usuario_model->get_all_empleados();
        $this->load->vars($data);
		$this->load->view('user_views/dashboard_user_view');
	}	

    function logout() {
        $this->session->sess_destroy();
        $this->session->unset_userdata('logged_in');
        $this->session->set_flashdata('error', "Usted ha sido deslogueado!");
        redirect('login', 'refresh');
    }
	
	function create_user() {        
        $data['title'] = "Nuevo Usuario";
        $data['username'] = $this->session->userdata('username');
        $data['name'] = $this->session->userdata('name');
        $data['acceso'] = $this->session->userdata('acceso');
		$data['perfiles'] = $this->get_empleado_profile();
        $this->load->vars($data);
        $this->load->view('user_views/create_user_view');
    }
	
	public function modificar($idUser) {          
        $data['title'] = "Modificar Usuario";
        $data['main'] = "login/login_box.php"; //RUTA		
        $data['username'] = $this->session->userdata('username');
        $data['name'] = $this->session->userdata('name');
        $data['acceso'] = $this->session->userdata('acceso');
        //OBTENIENDO DATA PARA PERFILES
        $data['perfiles'] = $this->get_empleado_profile();
        $data['usuario'] = $this->usuario_model->get_user_by_id($idUser);
		
        $this->load->vars($data);
        $this->load->view('user_views/edit_user_view');
        //echo "<script languaje='javascript'>alert('Index')</script>";
    }
	
	public function create(){
		$rules_create = 0;
        $this->set_validation_rules($rules_create);
		
		$data['Usuario'] = array(
            'Nombres' => xss_clean($this->input->post('firstname')),
            'Apellidos' => xss_clean($this->input->post('lastname1')),
            'DNI' => xss_clean($this->input->post('dni')),
            'Celular' => xss_clean($this->input->post('cellphone')),
            'Telefono' => xss_clean($this->input->post('phone')),
            'Email' => xss_clean($this->input->post('email')),
			'FechaNacimiento' => xss_clean($this->input->post('birthdate')),
            'Domicilio' => xss_clean($this->input->post('address')),
            'IdPerfil' => xss_clean($this->input->post('perfil_id')),
			'Usuario' => xss_clean($this->input->post('username')),
			'Password' => xss_clean($this->input->post('password')),
			'IdPerfil' => xss_clean($this->input->post('perfil_id')),
            'Activo' => '1'
        );
		
		$this->usuario_model->create($data['Usuario']);
		
		$data['Empleado'] = array(
			'idUsuario' => $this->usuario_model->get_last_iduser(),
			'idEmpresa' => '1',
			'idTipoEmpleado' => '2',
            'disponible' => '1'
        );
		
		$this->usuario_model->createEmpleado($data['Empleado']);
		
		$this->session->set_flashdata('message', 'El usuario ha sido creado correctamente.');
		
		redirect('/usuarios', 'refresh');	
	}
	
	public function edit($idUsuario = '') {
        //Parametro para setear reglas del edit
        $rules_edit = 1;
        $this->set_validation_rules($rules_edit);
        $data['username'] = $this->session->userdata('username');
        $data['name'] = $this->session->userdata('name');
        $data['acceso'] = $this->session->userdata('acceso');

        $data['Usuario'] = array(
			'idUsuario' => xss_clean($this->input->post('idUsuario')),
			'nombres' => xss_clean($this->input->post('firstname')),
            'apellidos' => xss_clean($this->input->post('lastname1')),
            'DNI' => xss_clean($this->input->post('dni')),
            'fechaNacimiento' => xss_clean($this->input->post('birthdate')),
            'telefono' => xss_clean($this->input->post('phone')),
			'celular' => xss_clean($this->input->post('cellphone')),
            'email' => xss_clean($this->input->post('email')),
			'usuario' => xss_clean($this->input->post('username')),
			'domicilio' => xss_clean($this->input->post('address')),
            'IdPerfil' => xss_clean($this->input->post('perfil_id')),
            'Activo' => $this->input->post('activo')
        );
		
		//print_r($data['Usuario']);
        echo '<pre>';
        // print_r ($this->input->post());

        $this->usuario_model->edit($this->input->post('idUsuario'), $data['Usuario']);

        $this->session->set_flashdata('message', 'Los datos del usuarios han sido modificados correctamente.');
        redirect('usuarios/', 'refresh');
    }
	
	public function set_validation_rules($rules) {
        if ($rules == 0) { //Rules para create
            $this->form_validation->set_rules('username', 'Nombre de Usuario', 'required|callback__check_username'); //**
            $this->form_validation->set_rules('firstname', 'Nombre', 'required');
            $this->form_validation->set_rules('lastname1', 'Apellidos', 'required');
			$this->form_validation->set_rules('address', 'Domicilio', 'required');
            $this->form_validation->set_rules('birthdate', 'Fecha de Nacimiento', 'required');
            $this->form_validation->set_rules('phone', 'Telefono', 'number');
			$this->form_validation->set_rules('cellphone', 'Celular', 'number');
            $this->form_validation->set_rules('password', 'Password', 'required|matches[passwordrepeat]|min_length[4]');
            $this->form_validation->set_rules('passwordrepeat', 'Repetir Password', 'required|min_length[4]');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback__check_email');
        }
        if ($rules == 1) { //Rules para edit
            $this->form_validation->set_rules('perfil', 'Perfil', 'required');
            $this->form_validation->set_rules('activo', 'Activo', 'required');
        }
        if ($rules == 2) { //Rules para info
        }
        if ($rules == 3) { //Rules para Change
        }
    }
	
	function get_empleado_profile() {
        $perfiles = array();
        $profiles = $this->perfil_model->get_empleado_profile();

        foreach ($profiles as $profile) {
            $perfiles[$profile['idPerfil']] = $profile['nombrePerfil'];
        }
        return $perfiles;
    }
}

?>
